<?php
namespace Home\Controller;

use Think\Controller;


class UserCenterController extends CommnController{

    public function __construct(){
        parent::__construct();
    }
    //ajax 返回数据   用户打进电话
    public function return_user(){
        $mobile=I('get.mobile');
        $flag=0;//非平台用户
        $user=M('user','','USER')->where(['mobile'=>$mobile])->Field('vid,truename,cert_status')->find();
        $exit=M('tmp_user','','KEFU')->where(['vid'=>$user['vid']])->field('status')->find();
        $exit_anti= M('anti_check_user','','KEFU')->where(['vid'=>$user['vid']])->find();
         if($exit['status']==1 && empty($exit_anti)){//存在反欺数据表 但是没有分配 加到已分配表
             $arr=array('vid'=>$user['vid'],'out_kf_id'=>$_SESSION['id'],'ctime'=>time());
             M('anti_check_user','','KEFU')->add($arr);
         }
        if(!empty($user)){
            $flag=1;//平台用户 进入个人信息
        }
        if(!empty($exit)){
            $flag=2;//反欺诈用户 进入反欺诈调查
        }
        echo $user['truename'].','.$flag.','.$user['vid'];
    }

    public function update_user_photo(){
        $vid=I('get.vid');
        $this->assign(array('vid'=>$vid));
        $this->display();
    }

    //修改身份证图片
    public function update_photo(){
         $vid=I('vid');
         $front=I('front');
         $anti=I('anti');
         if(empty($vid)  || $vid==''){
             $this->ajax('1002', '修改失败');
         }
        if(!empty($front) && $front!=''){
            $update=M('user_idcard_img','','USER')->where(['vid'=>$vid])->setfield('front',$front);
         }
         if(!empty($anti) && $anti!=''){
             $update= M('user_idcard_img','','USER')->where(['vid'=>$vid])->setfield('behind',$anti);
        }
        if($update){
            $this->ajax('1001', '修改成功');
            die;
        }
        $this->ajax('1002', '修改失败');
        die;
    }

    public function user_list_details(){
        $id = I('get.id');
        $user = M('user', '', 'USER')->where(['id' => $id])->field('vid,mobile,truename,id_card,icon,address,status,waitdays,statustype,
        checkstatus,work_cert,cert_status,zhima_verify,contact_verify,cert_time,ctime,
        usermsg_verify,mobile_verify,shebao_verify,jd_verify,credit_verify,check_type,money,quota,balance,freeze_balance')->find();
        $this->display();
    }

    public function user_list_info(){
        $search = I('get.user');
        $this->rid=I('rid');
        $this->roles = role(I('rid'));
        $this->user_content=$search;

        if(empty($search) || $search==''){
            $this->display();
            die;
        }

        $where['mobile|truename|id_card'] = array('eq', $search );
//        $where['mobile|truename|id_card'] = array('like', '%' . $search . '%');
        $tol = M('user', '', 'USER')->where($where)->count();
        $row = '12';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
         $user = M('user', '', 'USER')->where($where)->field('vid,id,mobile,truename,cert_status,id_card,ctime')->LIMIT($page->listfirst, $page->listRows)->order('ctime desc')->select();
            foreach ($user as $k => $v) {
                $borrow_time=M('user_borrow','','CAIWU')->where(['vid'=>$v['vid']])->field('ctime,id')->order('id desc')->find();
                if(empty($borrow_time)){
                    $user[$k]['borrow_time']='-';
                }else{
                    $user[$k]['borrow_time']=date('Y-m-d H:i',$borrow_time['ctime']);
                }
                if ($v['cert_status'] == '1') {
                    $user[$k]['cert_status'] = '未认证';
                } else if ($v['cert_status'] == '2'){
                    $user[$k]['cert_status'] = '已通过';
                } else if ($v['cert_status'] == '3') {
                    $user[$k]['cert_status'] = '未通过';
                }
                $len = strlen($v['id_card']);
                if (!empty($len)){
                    $start = substr($v['id_card'], 0, 6);
                    $end = substr($v['id_card'], $len - 4, $len);
                    $user[$k]['id_card1'] = $start . '*******' . $end;
                }
                $user[$k]['ctime']=date('Y-m-d H:i' ,$v['ctime']);
                //是否优质  借款次数三次以上 无逾期
                $w=[];
                $w['borrow_num']=array('egt',3);
                $w['overdue_num']=array('eq',0);
                $w['vid']=array('eq',$user[$k]['vid']);
                $data=M('user_borrow_statistics','','USER')->where($w)->field('id')->find();
                if(empty($data)){
                    $user[$k]['excellent']='否';
                }else{
                    $user[$k]['excellent']='优质';
                }
            }
            $this->assign(array(
                'user' => $user,
                'fpage' => $fpage,
                'tol' => $tol
            ));
            $this->page=I('page');
            $this->display();
    }
    //沟通记录
    public function communicate_record1(){
        $this->page1=I('page1');
        $this->rid=I('rid');
        $this->user_content=I('user_content');
        $vid = I('get.vid');
        $row = '10';
        $search = I('get.user');
        $type = I('get.type',1);
        $where = [];
        $where['b.vid']=$vid;
        if (!empty($search)) {
            $where['a.content'] = array('like', '%' . $search . '%');
        }
        $tol =M('user', '', 'USER')->join('b join dxd_kefu.user_feedback a on b.vid=a.vid')->where($where)->count();
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        if($type==1){
            $user = M('user', '', 'USER')->join('b join dxd_kefu.user_feedback a on b.vid=a.vid')->where($where)->limit($page->listfirst, $page->listRows)->field('a.id,a.vid,a.content,a.ctime,a.status')->select();
            foreach ($user as $k=>$v){
                $meesage=M('user_message','','KEFU')->where(['uf_id'=>$v['id'],'vid'=>'0'])->field('admin_name,content')->find();
                $user[$k]['kefu_name']=$meesage['admin_name'];
                $user[$k]['back_content']=$meesage['content'];
                $user[$k]['ctime']=date('Y-m-d H:i',$v['ctime']);
                if($v['status']==3){
                    $user[$k]['status']='解决';
                }else{
                    $user[$k]['status']='未解决';
                }
            }
        }

        $this->assign(array(
            'user' => $user,
            'fpage' => $fpage,
            'tol' => $tol,
            'type' => $type,
            'vid' => $vid
        ));

        $this->display();
    }

    //沟通记录
    public function user_update(){
        $vid = I('get.vid');

        $user = M('user', '', 'USER')->where(['vid' => $vid])->field('vid,mobile,truename,id_card,province,city,ismarry,sex,ctime,address,degree,residence_time')->find();
        $user_pic=M('user_idcard_img','','USER')->where(['vid'=>$user['vid']])->field('photo,photo1,photo2,front,behind')->find();
        $user['ctime']=date('Y-m-d H:i',$user['ctime']);
        $user['card_address']=$user['province'].$user['city'];
        $this->assign(array(
            'user' => $user,
            'user_pic' => $user_pic,
            'vid' => $vid
        ));

        $this->display();
    }

    //用户信息修改 保存
    public function user_save(){
        $vid = I('vid');
        $user_name = I('user_name');
        $select_sex = I('select_sex');
        $select_marry = I('select_marry');
        $degree= I('degree');
        $id_card = I('id_card');
        $live_address = I('live_address');
        $live_time = I('live_time');

        $arr=array('truename'=>$user_name,'sex'=>$select_sex,'ismarry'=>$select_marry,'degree'=>$degree,'id_card'=>$id_card,'address'=>$live_address,'residence_time'=>$live_time);

        $user = M('user', '', 'USER')->where(['vid' => $vid])->save($arr);
        if($user){
            $this->ajax('1001', '修改成功');
        }else{
            $this->ajax('1002', '修改失败');
        }


    }

}

